module InputHelper
  
  def get_play
    prompt = @players_play ? "What did you play ?" : "What did the opponent do ?"
    puts prompt
    gets
  end
  
  def get_first_player
    puts "Are you first? (y/n)"
    ans = gets.chomp
    ans.downcase == 'y'
  end
  
  def set_initial_score
    puts "initial score?"
    ans = gets.split " "
    @player.points = ans[0].to_i
    @opp.points = ans[1].to_i
  end
  
  def handle_play_input(inp)
    parts = inp.split " "
    case parts.size
    when 1
      case parts[0]
      when "hand"
        puts "#{@player.hand}"
        false
      when "pool"
        puts "#{@pool}"
        false
      when "board"
        puts "#{@board}"
        false
      when "prob_hash"
        puts "#{@prob_hash}"
        false
      when "fdraw"
        force_draw
        false
      when "odraw"
        opponent_draw
        false
      when "pass"
        @players_play ? player_pass : opponent_pass
      when "best"
        p = get_best_move
        puts "Best move is #{p}"
        false
      else
        false
      end
    when 2 # a single domino -- only for game beginning
      if @board.game_started?
        false
      else
        d = Domino.new parts[0].to_i, parts[1].to_i
        play_domino d, DominoesGame::WILDCARD
      end
    
    when 3
      false
    when 4
      if @board.game_started?
        domino = Domino.new parts[0].to_i, parts[1].to_i
        dominee = Domino.new parts[2].to_i, parts[3].to_i
        play_domino domino, dominee
      else
        false
      end
    when 5
      if @board.game_started?
        if parts[4] == "s"
          domino = Domino.new parts[0].to_i, parts[1].to_i
          dominee = Domino.new parts[2].to_i, parts[3].to_i, true
          play_domino domino, dominee  
        else
          false
        end
      else
        false
      end  
    else
      puts "unrecognized input: #{inp}"
      false
    end
  end
  
  def force_draw
    puts "what did you draw?"
    while (inp = gets.split(" ")).size == 2
      d = Domino.new inp[0].to_i, inp[1].to_i
      @player.force_draw(d)
    end
  end
  
  def player_pass
    true
  end
  
  def opponent_pass
    remove_valids_from_prob_hash
    true
  end
    
  def opponent_draw
    puts "how many did the opp draw?"
    s = gets.to_i
    @opp.draw s
    #@prob_hash.reset_stickies (s.to_f/@pool.size.to_f).round(4) #can be better as not all dominoes equally likely to be in pool
    
    prob_of_draw = (s.to_f/@pool.size.to_f).round(4)
    @prob_hash.reset do |d| 
      if d.sticky
        prob_in_pool = 1 - d.value 
        d.value = prob_in_pool * prob_of_draw 
      end
    end
    
    remove_valids_from_prob_hash
  end
  
end